package edu.hcmus.sow.web;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import edu.hcmus.sow.service.RepositoryService;

@Controller
public class ResourceController {
   // ~ Instance fields
   // ================================================================================================
   @Autowired
   private RepositoryService repositoryService;

   // ~ Methods
   // ================================================================================================
   @RequestMapping("/resource/{key}")
   public void download(@PathVariable("key") String key, @RequestParam("disposition") String disposition,
         HttpServletResponse response) {

      // response.setContentType(file.getType());
      // response.setContentLength((int) file.getSize().longValue());
      response.setHeader("", "attachment;filename=");

      try {
         repositoryService.readFile(key, response.getOutputStream());
         response.flushBuffer();
      } catch (Exception e) {
         e.printStackTrace();
      }

   }
}
